package com.gears_software.sxdc.dccx.ui.fragment;


import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.content.ContextCompat;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.gears_software.sxdc.dccx.R;
import com.gears_software.sxdc.dccx.application.DCCXApplication;
import com.gears_software.sxdc.dccx.data.adapter.FragmentAdapter;
import com.gears_software.sxdc.dccx.data.sharedPreference.DCCXSp_;
import com.gears_software.sxdc.dccx.ui.activity.SelectorLineActivity_;
import com.gears_software.sxdc.dccx.ui.util.popu.SortPopup;
import com.gigamole.navigationtabstrip.NavigationTabStrip;
import com.github.mzule.activityrouter.router.Routers;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.App;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EFragment;
import org.androidannotations.annotations.OnActivityResult;
import org.androidannotations.annotations.ViewById;
import org.androidannotations.annotations.sharedpreferences.Pref;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by chezhenzhen on 2017/6/26.
 * 拼车
 */
@EFragment(R.layout.fragment_carpool)
public class CarPoolFragment extends Fragment {

    @App
    DCCXApplication app;
    @ViewById
    TextView txt_sx_icon;
    @ViewById
    TextView txt_sort_icon;
    @ViewById
    TextView txt_sort;
    @ViewById
    LinearLayout ly_sx;
    @ViewById
    LinearLayout ly_sj;
    @ViewById
    NavigationTabStrip carpool_navigationTabStrip;
    @ViewById
    ViewPager carpool_viewpager;
    @Pref
    DCCXSp_ sp;
    String[] carpool_titles = {"乘客", "车主"};
    List<Fragment> fragments = new ArrayList<Fragment>();
    FragmentAdapter adapter;
    PassengerFragment pf;

    @AfterViews
    void created() {
        initIconFont();
        initViewPager();
        initNavTabStrip();
        if (sp.sortTag().get() != null && !sp.sortTag().get().equals("")) {
            txt_sort.setText(sp.sortTag().get());
        }
    }

    void initIconFont() {
        txt_sx_icon.setTypeface(app.iconfont);
        txt_sort_icon.setTypeface(app.iconfont);
    }

    void initViewPager() {
        pf = new PassengerFragment_();
        fragments.add(pf);
        fragments.add(new CarOwnerFragment_());
        adapter = new FragmentAdapter(getFragmentManager(), fragments, carpool_titles);
        carpool_viewpager.setAdapter(adapter);
        carpool_viewpager.setCurrentItem(0);
    }

    void initNavTabStrip() {
        carpool_navigationTabStrip.setTitles(carpool_titles);
        carpool_navigationTabStrip.setTitleSize(getResources().getDimensionPixelSize(R.dimen.small_font_size));
        carpool_navigationTabStrip.setStripColor(ContextCompat.getColor(getActivity(), R.color.colorActionbar));
        carpool_navigationTabStrip.setStripWeight(2);
        carpool_navigationTabStrip.setStripFactor(2);
        carpool_navigationTabStrip.setStripGravity(NavigationTabStrip.StripGravity.BOTTOM);
        carpool_navigationTabStrip.setStripType(NavigationTabStrip.StripType.LINE);
        carpool_navigationTabStrip.setTypeface("fonts/typeface.ttf");
        carpool_navigationTabStrip.setCornersRadius(3);
        carpool_navigationTabStrip.setAnimationDuration(300);
        carpool_navigationTabStrip.setInactiveColor(ContextCompat.getColor(getActivity(), R.color.colorTabFont));
        carpool_navigationTabStrip.setActiveColor(ContextCompat.getColor(getActivity(), R.color.colorActionbar));
        carpool_navigationTabStrip.setViewPager(carpool_viewpager, 0);
        carpool_navigationTabStrip.setOnTabStripSelectedIndexListener(new NavigationTabStrip.OnTabStripSelectedIndexListener() {
            @Override
            public void onStartTabSelected(String title, int index) {
//                TastyToast.makeText(getActivity(),"start"+title,TastyToast.LENGTH_SHORT,TastyToast.INFO);
            }

            @Override
            public void onEndTabSelected(String title, int index) {
                if (index == 1) {
                    ly_sj.setVisibility(View.GONE);
                    ly_sx.setVisibility(View.GONE);
                } else {
                    ly_sj.setVisibility(View.VISIBLE);
                    ly_sx.setVisibility(View.VISIBLE);
                }
//                TastyToast.makeText(getActivity(), "end" + title + String.valueOf(index), TastyToast.LENGTH_SHORT, TastyToast.INFO);

            }
        });
    }

    @Click
    void ly_sj() {
        SortPopup sortPopup = new SortPopup(getActivity());
        sortPopup.setSortClick(new SortPopup.SortcallBack() {
            @Override
            public void sortClick(String sortTag) {
                txt_sort.setText(sortTag);
                if (sortTag.equals("时间")) {
                    pf.sort = "-departtime";
                } else {
                    pf.sort = "price";
                }
                pf.refresh();
            }
        });
        sortPopup.showPopupWindow(R.id.re_top);
    }

    @Click
    void ly_sx() {
        sp.startPoint().put("");
        sp.endPorint().put("");
        sp.direction().put(0);
        sp.departtime().put("");
        Intent intent = new Intent(getActivity(), SelectorLineActivity_.class);
        startActivityForResult(intent, 1);
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 1) {
            pf.isInitRoute();
            pf.refresh();
        }
    }
}
